var gulp = require("gulp");
var htmlmin = require('gulp-htmlmin');
var cssmin = require("gulp-minify-css");
var uglify = require("gulp-uglify");
var concat = require('gulp-concat');
var autoprefixer = require('gulp-autoprefixer');
var sass = require("gulp-sass");
var clean = require("gulp-clean");

var version = 'dist/' + 20180122;

var srcPath = {
  html: 'src/**/*.html',
  scss: 'src/**/*.scss',
  js: 'src/**/*.js',
  image: 'src/**/*.(png||jpg||gif)'
};

var distPath = {
  html: version + '/**/*.html',
  scss: version + '/**/*.scss',
  js: version + '/**/*.js',
  image: version + '/**/*.(png||jpg||gif)'
};
var libs = [ 'src/lib/bootstrap.js', 'src/lib/jquery-1.11.3.js' ];

gulp.task('html', function () {
  gulp.src(srcPath.html)
    .pipe(htmlmin())
    .pipe(gulp.dest(version));
});

gulp.task('sass', function () {
  return gulp.src(srcPath.scss)
    .pipe(sass())
    .pipe(autoprefixer({//加前缀
      browsers: [ 'last 2 versions', 'Android >= 4.0' ],
    }))
    .pipe(cssmin())
    .pipe(gulp.dest(version));
});

gulp.task('js', function () {
  gulp.src([ srcPath.js, '!src/lib/**/*.js' ])
    .pipe(uglify())
    .pipe(gulp.dest(version));
});

gulp.task('image', function () {
  gulp.src(srcPath.image)
    .pipe(gulp.dest(version));
});

gulp.task('concatLib', function () {
  gulp.src(libs)
    .pipe(concat('lib.min.js'))
    .pipe(uglify())
    .pipe(gulp.dest(version + '/lib'));
});

// 清除制定目录
gulp.task('clean', function () {
  return gulp.src(version)
    .pipe(clean());
});

gulp.task('sass:watch', function () {
  gulp.watch('src/style/*.scss', [ 'sass' ]);
});

gulp.task('dev', [ 'sass:watch', 'html', 'sass', 'js', 'image', 'concatLib' ]);

gulp.task('pub', [ 'clean', 'html', 'sass', 'js', 'image', 'concatLib' ]);

